/**
 * Created by hkf6644 on 2017/5/24.
 */
var departmentPageObj = {
    search : {
        value : ''
    },
    datas : [],
    addParamObj : {
        outId : '',
        parentId : '',
        code : '',
        name : '',
        description : ''
    },
    showModel : false,
    title : '',
    addTitle : '',
    parentData : []
};

Vue.component('department-page', {
    props: [],
    template : CommonFunc.ajax.getOuterHtml('component/department_list.html'),
    data: function () {
        return departmentPageObj
    },
    methods: {
        searchImf : function () {

        },
        showModal : function (type,title) {
        	this.addParamObj.deptId = '';
        	
            departmentPageObj.title = '新增部门';
            this.showModel = true;
        }
    }
});

var getDeptList = function (ouId,deptId) {
    var deptListUrl = '/cas/console/dept/querydeptTree.ajax';

    var deptListCallback = function (rs) {
        if (rs.isSuccess) {
            var data = rs.data;

            if (ouId) {
                userAddObj.deptData = [];

                if (data && typeof data !== 'string') {
                    userAddObj.deptData = handleParentData(data,true);
                }
            } else {
            	if (deptId) {
            		departmentPageObj.parentData = handleParentData(data,false,deptId);
            	} else {
                	departmentPageObj.parentData = handleParentData(data);
            	}
                departmentPageObj.datas = data;
            }
        } else {
            vmdata.errorAlert = rs.errMsg;
        }
    };

    if (!ouId) {
        var ouId1 = departmentPageObj.addParamObj.outId;
    } else {
        var ouId1 = ouId;
    }

    $.get(deptListUrl,{ouId:ouId1},deptListCallback, "json");
};

var handleParentData = function (data,isgetList,deptId) {
    var parentData = [];

    var handle = function (data1) {
        var parentData1 = data1.map(function(value) {
        	if (value.deptId === deptId) {
				return false;
			} else {
				var obj = {};
	            obj.value = value.deptId;
	            obj.label = value.name?value.name:'没有名称';
	            obj.children = [];
	
	            if (value.subDeptList && typeof value.subDeptList !== 'string' && value.subDeptList.length > 0) {
	                obj.children = handle(value.subDeptList);
	            }
	
	            return obj;
			};
        });

        return parentData1;
    };

    if (data && typeof data !== 'string' && data.length > 0) {
		parentData = handle(data);
    }

	if (!isgetList) {
        parentData.unshift({label: '顶级部门',value: '', children: []});
    }
	
	if (deptId) {
		var handle2 = function (parentData1) {
			parentData1.forEach(function (value,index,arr) {
				if (value === false) {
					arr.splice(index,1);
				} else {
					if (value.children && typeof value.children !== 'string' && value.children.length > 0) {
		                handle2(value.children);
		            }
				}
			});
		}
		handle2(parentData);
	}
	
    return parentData;
};

Vue.component('departmentTable', {
    props: ['datas'],
    name: 'departmentTable',
    template : CommonFunc.ajax.getOuterHtml('component/department_table.html'),
    data: function () {
        return {}
    },
    methods : {
        editorDept : function (obj,e) {
            var thisE = $(e.target);
            
            getDeptList(false,obj.deptId);

            var handleVlue1 = function (E) {
                if (E.hasClass('ivuTr')) {
                    var value = E.attr('data-deptId');
                } else {
                    var value = E.prev().attr('data-deptId');
                }
                if (value) {
                    addObj.value1.unshift(value);

                    if (E.parent().closest('.ivueUlBox')) {
                        handleVlue1(E.parent().closest('.ivueUlBox'));
                    }
                }
            };

            if (thisE.closest('.ivueUlBox')) {
                handleVlue1(thisE.closest('.ivueUlBox'));
            }

            if (addObj.value1.length > 0) {
                addObj.value1.splice(addObj.value1.length - 1, 1);
            }

            departmentPageObj.addParamObj.deptId = obj.deptId;
            departmentPageObj.addParamObj.parentId = obj.parentId;
            departmentPageObj.addParamObj.code = obj.code;
            departmentPageObj.addParamObj.name = obj.name;
            departmentPageObj.addParamObj.description = obj.description;

            departmentPageObj.showModel = true;
            departmentPageObj.title = '编辑部门';
        },
        deleteDept : function (deptId,deptName) {
            var okFunc = function () {
                var deleteUrl = '/cas/console/dept/delete.ajax';

                var deleteCallback = function (rs) {
                    if (rs.isSuccess) {
                        vmdata.successAlert = '删除部门成功！';
                        getDeptList();
                    } else {
                        vmdata.errorAlert = rs.errMsg;
                    }
                };

                $.get(deleteUrl,{deptId : deptId}, deleteCallback);
            };

            this.$Modal.confirm({
                title : '删除提示',
                content : '确认删除' + deptName + '？',
                onOk : okFunc
            });
        }
    }
});

var addObj = {
    value1 : []
};

Vue.component('departmentAdd', {
    props: ['addParamObj', 'data', 'title'],
    template : CommonFunc.ajax.getOuterHtml('component/department_add.html'),
    data: function () {
        return addObj
    },
    methods : {
        saveDept : function () {
            var _this = this;

            this.addParamObj.parentId = this.value1[this.value1.length - 1];

            if (_this.addParamObj.deptId) {
                var addUrl = '/cas/console/dept/edit.ajax';
            } else {
                var addUrl = '/cas/console/dept/add.ajax';
            }
            
            if (!this.addParamObj.parentId && this.addParamObj.parentId !== '') {
            	vmdata.warningAlert = '请选择父部门!';
            	return false;
            }
            
            if (!this.addParamObj.code) {
            	vmdata.warningAlert = '请填写部门编码!';
            	return false;
            }
            if (!this.addParamObj.name) {
            	vmdata.warningAlert = '请填写部门名称!';
            	return false;
            }
            if (!this.addParamObj.description) {
            	vmdata.warningAlert = '请填写部门描述!';
            	return false;
            }

            var addCallback = function (rs) {
                if (rs.isSuccess) {
                    if (_this.addParamObj.deptId) {
                        vmdata.successAlert = '编辑部门成功！';
                    } else {
                        vmdata.successAlert = '新建部门成功！';
                    }

                    getDeptList();
                    _this.value1 = [];

                    _this.addParamObj.parentId = '';
                    _this.addParamObj.code = '';
                    _this.addParamObj.name = '';
                    _this.addParamObj.description = '';

            		departmentPageObj.title = '';
                    departmentPageObj.showModel = false;
                } else {
                    vmdata.errorAlert = rs.errMsg;
                }
            };

            $.ajax({
                type: "POST",
                url: addUrl,
                async: true,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(this.addParamObj),
                success: addCallback
            });
        },
        cancleDept : function () {
            this.value1 = [];

            this.addParamObj.parentId = '';
            this.addParamObj.code = '';
            this.addParamObj.name = '';
            this.addParamObj.description = '';

            departmentPageObj.title = '';
            departmentPageObj.showModel = false;
        }
    }
});

